package com.yesjpt.serv.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yesjpt.serv.AliSmsServ;

@Service
public class AliSmsServImpl implements AliSmsServ {

	private static String accessKeyId = "key_001";//申请
	
	private static String accessKeySecret = "123456";//申请
	
	private static String SignName = "陈剑波";//申请

	private DefaultProfile profile = DefaultProfile.getProfile(
			"cn-hangzhou", accessKeyId, accessKeySecret);
	private IAcsClient client = new DefaultAcsClient(profile);
	
	@Override
	/*
	 * phoneNumbers：目标手机
	 * json：模板中的参数值，比如模板为【验证码为：${code}】，则json = {code: 123456}
	 */
	public String sendVerifyCode(String phoneNumbers, JSONObject json) {
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", phoneNumbers);
		request.putQueryParameter("SignName", SignName);//申请的时候签名
		request.putQueryParameter("TemplateCode", "申请的模板名");
		request.putQueryParameter("TemplateParam", json.toJSONString());
		try {
			CommonResponse commonResponse = client.getCommonResponse(request);
			String resp = commonResponse.getData();
			System.out.println(resp);
			return resp;
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return null;
	}
	/*
	private String mapToJson(Map<String, Object> map) {
		StringBuilder sb = new StringBuilder("{");
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			sb.append(entry.getKey()).append(":").append(entry.getValue()).append(",");
		}
		sb.substring(0, sb.length() - 1);
		sb.append("}");
		return String.valueOf(sb);
	}
	*/
}
